<?php

namespace Mem;

use MemLib\MemBase;
use MemLib\Pdb;

/**
 * 订阅详情信息表
 *
 * @version 1.0
 * @author 张云江
 */
class MemOrder_Subscribe_info extends MemBase
{
    public function __construct()
    {
        parent::__construct(Pdb::db_order, "t_order_subscribe_info");
    }

    /**
     * 插入数据
     * @param mixed $product_id 产品ID
     * @param mixed $application_id t_order_application_info 应用ID
     * @param mixed $transaction_id 苹果订阅编号
     * @param mixed $state 订阅状态 1.订阅中 0.取消订阅
     * @param mixed $expiration_intent 取消原因 1.用户自动取消 2
     */
    public function insertDataWithParam($product_id, $application_id, $transaction_id, $state, $expiration_intent)
    {
        $insert_dt = date("Y-m-d H:i:s", time());
        $data = array(
            "product_id" => $product_id,
            "application_id" => $application_id,
            "transaction_id" => $transaction_id,
            "state" => $state,
            "expiration_intent" => $expiration_intent,
            "insert_dt" => $insert_dt
        );
        $res = $this->addData($data);
        if ($res) {
            \Mlog::info("t_order_subscribe_info 插入数据成功" . $transaction_id);
        }
        return $this->getTmpID();
    }

    /**
     * 根据订单详情
     * @param mixed $subscribe_id
     * @return array
     */
    public function getInfoWithsubscribe_id($subscribe_id)
    {
        $sql = "SELECT * FROM t_order_subscribe_info WHERE subscribe_id='{$subscribe_id}';";
        $data = $this->getData($sql, "getInfoWithsubscribe_id:{$subscribe_id}");
        return $data ? $data[0] : array();
    }

    /**
     * 更新信息
     * @param $updata
     * @param $wheredata
     * @return bool
     */
    public function updateDataWithParam($updata, $wheredata)
    {
        if (empty($updata) || empty($wheredata)) {
            return false;
        }

        $updata = \MemLib\PublicFunc::convertToHtmlKeyVal($updata);
        $wheredata = \MemLib\PublicFunc::convertToHtmlKeyVal($wheredata);

        $ret = $this->updateData($updata, $wheredata);

        return $ret;
    }

    /**
     * 根据订单详情
     * @param double $transaction_id
     * @return array
     */
    public function getInfoWithtransaction_id($transaction_id)
    {
        $sql = "SELECT * FROM t_order_subscribe_info WHERE transaction_id='{$transaction_id}';";
        $data = $this->getData($sql, "getInfoWithtransaction_id:{$transaction_id}");
        return $data ? $data[0] : array();
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getCountDataWithAll($application_id, $type, $stxt)
    {
        $whereStr = '';
        if ($application_id > 0) {
            $whereStr = $whereStr . ' AND application_id = ' . $application_id;
        }
        if (!empty($stxt)) {
            if ($type == 1) {
                $whereStr = $whereStr . " AND transaction_id = '" . $stxt . "'";
            } else {
                $whereStr = $whereStr . " AND product_id = '" . $stxt . "'";
            }
        }
        $sql = "SELECT COUNT(*) AS total FROM t_order_subscribe_info WHERE subscribe_id != 0 {$whereStr};";
        $data = $this->getData($sql, "getCountDataWithAllapplication_id{$application_id}:type:{$type}:stxt:{$stxt}");
        return empty($data) ? $data : $data[0]['total'];
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getDataWithAll($application_id, $type, $stxt, $mempage)
    {
        $application_id = (int)$application_id;
        $whereStr = '';
        if ($application_id > 0) {
            $whereStr = $whereStr . ' AND pcl.application_id = ' . $application_id;
        }
        if (!empty($stxt)) {
            if ($type == 1) {
                $whereStr = $whereStr . " AND pcl.transaction_id = '" . $stxt . "'";
            } else {
                $whereStr = $whereStr . " AND pcl.product_id = '" . $stxt . "'";
            }
        }
        $sql = "SELECT pcl.*,pclm.application_name,pcld.product_time,pcld.product_name FROM db_order.t_order_subscribe_info AS pcl LEFT JOIN db_order.t_order_product_info AS pcld ON pcld.product_channel = pcl.product_id LEFT JOIN db_order.t_order_application_info AS pclm ON pclm.application_id = pcl.application_id WHERE pcl.subscribe_id != 0 {$whereStr} order by pcl.insert_dt desc LIMIT {$mempage->getLimitIndex()},{$mempage->pagesize};";
        $data = $this->getData($sql, "getDataWithAllapplication_id{$application_id}:type:{$type}:stxt:{$stxt}AndPage:{$mempage->getLimitIndex()}:pagesize:{$mempage->pagesize}");
        return $data;
    }
}
